﻿/*---------------------------------------------------------------------*/
// fileName="NotifyRes.cs" CompanyName="SureKAM"
// Copyright SureKAM (c). All right reserved.
/*---------------------------------------------------------------------*/

namespace SureKAM.BackUp.Notify.DataAccess.implement
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.Practices.Unity;

    /// <summary>
    /// 通知者数据处理类
    /// </summary>
    public class NotifyRes : INotify
    {
        #region 私有字段
        private List<Notify.Domain.Notify> notifies=null;
        private XMLHelper xmlHelper=null;
        #endregion

        #region 构造
        /// <summary>
        /// 构造方法
        /// </summary>
        public NotifyRes()
        {
            this.xmlHelper = new XMLHelper(AppDomain.CurrentDomain.BaseDirectory + "ConfigFile\\Notifyer.xml");
            this.notifies = this.xmlHelper.GetNodes<Notify.Domain.Notify>();
        }
        #endregion

        #region 公有方法
        /// <summary>
        /// 添加通知者对象
        /// </summary>
        /// <param name="notify">通知者对象</param>
        /// <returns>返回操作返回ID</returns>
        public string InsertNotify(Domain.Notify notify)
        {
            string objectValue = null;
            try
            {
                this.notifies.Add(notify);
                objectValue = notify.ID;
            }
            catch (Exception)
            {
                objectValue = null;
            }
            return objectValue;
        }

        /// <summary>
        /// 获取所有通知者对像
        /// </summary>
        /// <returns>返回所有通知者</returns>
        public List<Domain.Notify> GetNotifys()
        {
            return (this.notifies.Count > 0 ? this.notifies : null);
        }

        /// <summary>
        /// 更新相关通知者对象
        /// </summary>
        /// <param name="notify">通知者</param>
        /// <returns>返回更新后的通知者</returns>
        public Domain.Notify UpdateNotify(Domain.Notify notify)
        {
            try
            {
                for (int i = 0; i < this.notifies.Count; i++)
                {
                    if (notifies[i].ID == notify.ID)
                    {
                        notifies[i].Name = notify.Name;
                        notifies[i].Email = notify.Email;
                    }
                }
            }
            catch (Exception)
            {
                return null;
            }
            return notify;
        }

        /// <summary>
        /// 根据ID获取通知者
        /// </summary>
        /// <param name="id">通知者ID</param>
        /// <returns>返回相应的通知者</returns>
        public Domain.Notify GetNotifyById(string id)
        {
            var selectResult = from p in notifies where p.ID == id select p;
            return ((selectResult != null && selectResult.Count() > 0) ? selectResult.FirstOrDefault() : null);
        }

        /// <summary>
        /// 删除相关通知者对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteNotify(string id)
        {
            int i = 0;
            for (; i < this.notifies.Count; i++)
            {
                if(this.notifies[i].ID==id)
                {
                    break;
                }
            }
            if(i!=this.notifies.Count)
            {
                this.notifies.Remove(this.notifies[i]);
                return true;
            }
            return false;
        }

        /// <summary>
        /// 将数据写入到XML文件
        /// </summary>
        /// <returns></returns>
        public bool SaveToFile()
        {
            return this.xmlHelper.SaveToFile<Notify.Domain.Notify>(this.notifies);
        }
        #endregion        
    }
}
